//
//  HMStatusPictureView.m
//  OC新浪微博
//
//  Created by wyman on 16/4/16.
//  Copyright © 2016年 wyman. All rights reserved.
//

#import "HMStatusPictureView.h"
#import "HMPictureViewCell.h"
#import <SDWebImage/SDImageCache.h>
#import <UIImageView+WebCache.h>
#define HMStatusPictureViewMaxColumn 3
#define HMStatusPictureViewCellMargin 4

#define HMStatusPictureViewItemWH  ([UIScreen mainScreen].bounds.size.width - 2*HMStatusCellMargin - (HMStatusPictureViewMaxColumn-1)*HMStatusPictureViewCellMargin)/HMStatusPictureViewMaxColumn


@interface HMStatusPictureView()<UICollectionViewDataSource>
@property (nonatomic, strong) UICollectionViewFlowLayout *layout;
@end
@implementation HMStatusPictureView


-(instancetype)init{
    if (self = [super initWithFrame:CGRectZero collectionViewLayout:self.layout]) {
//        self.backgroundColor = [UIColor colorWithRed:((float)arc4random_uniform(256) / 255.0) green:((float)arc4random_uniform(256) / 255.0) blue:((float)arc4random_uniform(256) / 255.0) alpha:1.0];
    }
    
    self.backgroundColor = [UIColor clearColor];
    self.dataSource = self;

    [self registerClass:[HMPictureViewCell class] forCellWithReuseIdentifier:@"QQ"];
    return self;
}

-(void)setPictureURLs:(NSArray *)pictureURLs
{
    _pictureURLs = pictureURLs;
    
    

    CGSize size = [self callViewSize];
//        HMLog(@"配图大小 %f  %f",size.width,size.height);
    [self mas_updateConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(size);
        
        //复用问题
        [self reloadData];
}];

    
}
-(CGSize)callViewSize{

    self.layout.itemSize = CGSizeMake(HMStatusPictureViewItemWH, HMStatusPictureViewItemWH);

    self.layout.minimumInteritemSpacing = 0;
    self.layout.minimumLineSpacing = 0;
    
    // 判断图片的数量
    int count = (int)self.pictureURLs.count;
    
    // 没有图片
    if (count == 0) {
//        NSLog(@"没有图片");
        return CGSizeZero;
    }
    
    // 1.张图片
    if (count == 1) {
        //读取图片大小
        
        CGSize size = CGSizeMake(180, 90);

        NSURL *url = self.pictureURLs.firstObject ;
       UIImage *image =  [[[SDWebImageManager sharedManager] imageCache] imageFromDiskCacheForKey: [url absoluteString]];
        
        size = image.size;
        
        size = CGSizeMake(size.width*[UIScreen mainScreen].scale, size.height*[UIScreen mainScreen].scale);
        //计算像素点
        self.layout.itemSize = size;

        
        //计算大小
        // TODO: 返回图片本来的大小
        return size;
    }
    
    self.layout.minimumInteritemSpacing = HMStatusPictureViewCellMargin;
    self.layout.minimumLineSpacing = HMStatusPictureViewCellMargin;
    // 多张图片,列数不确定
    // 2张和4图片显示2列,其他都显示3列
    int column = HMStatusPictureViewMaxColumn;
    if (count == 2 || count == 4) {
        column = 2;
    }
    
    // 服务器开发用得比较多,分页,保证列数是整数
    // 2 + 2 - 1 / 2 = 1
    // 3 + 3 - 1 / 3 = 1
    // 4 + 2 - 1 / 2 = 2
    // 5 + 3 - 1 / 3 = 2
    // 6 + 3 - 1 / 3 = 2
    // 7 + 3 - 1 / 3 = 3
    // 行数 = (总个数 + 列数 - 1) / 列数
    int row = (count + column - 1) / column;
    
    // 计算宽度和高度
    // 宽度 = 列数 * item宽度 + (列数 - 1) * 列间距
  

    CGFloat widht = column*HMStatusPictureViewItemWH + (column-1)*HMStatusPictureViewCellMargin;
    // 高度度 = 行数 * item高度 + (行数 - 1) * 行间距
    CGFloat heigt = row * HMStatusPictureViewItemWH + (row-1)*HMStatusPictureViewCellMargin;
    HMLog(@"%zd",heigt);

    return CGSizeMake(widht, heigt);

}

-(void)prepareUI
{

    


}







-(UICollectionViewFlowLayout *)layout{
    if (_layout == nil) {
        _layout = [[UICollectionViewFlowLayout alloc]init];
    }
    return _layout;

}

#pragma mark - 数据源方法
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{

    return self.pictureURLs.count;
    
//    HMLog("%d",self.pictureURLs.count);
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{

    HMPictureViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"QQ" forIndexPath:indexPath];
  
    
    cell.imageUrl = self.pictureURLs[indexPath.item];
    
//    HMLog("%@",cell.imageUrl);
    return cell;
}
@end
